const Sequelize = require('sequelize')
const {sequelize} = require('./utils')
const {ProjectType} = require('@/mysql/project_type')

// 创建用户实体
const Project = sequelize.define('projects', {
    project_id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    name: {
        type: Sequelize.STRING,
        allowNull: false,
        unique: false
    },
    description: {
        type: Sequelize.STRING
    },
    deleted: {
        type: Sequelize.BOOLEAN,
        defaultValue: false
    },
    status: {
        type: Sequelize.INTEGER,
        defaultValue: 0
    },
    projectIcon: {
        type: Sequelize.STRING,
        allowNull: true
    }
});

Project.belongsTo(ProjectType)

/**插入新项目数据（添加）(不带用户) */
Project.insertData = async (name, description, type_id) => {
    return Project.create({name, description, projectTypeTypeId: type_id});
}
// force: true 如果表已经存在，将会丢弃表
Project.sync({force: false})
/** 获取所有用户(这个不是真正的获取项目的方法) */
Project.getAllProject = async () => {
    return Project.findAll({
        include:[{
            model: ProjectType,
            through: {
                attributes: ['name', 'type_id'],
                where: {type_id: Sequelize.col('projectTypeTypeId')}
            },
        }]
    })

}
/** 获取用户 */
Project.getById = async (id) => {
    return Project.findAll({where: {project_id: id}})
}
exports.Project = Project
